import {
  BookModule
} from '../../../modules/book.js'
let bookModule = new BookModule()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    history: [],
    keyWord: null,
    searchList: null,
    showSearch: false,
    value: ''
  },
  attached: function() {
    wx.getStorage({
      key: 'history',
      success: res => {
        let localHistory = res.data.history
        console.log(res)
        this.setData({
          history: localHistory.split("-")
        })
      }
    })
    bookModule.hotKeyword((res) => {
      this.setData({
        keyWord: res.hot
      })
    })
  },


  /**
   * 组件的方法列表
   */
  methods: {
    cancel: function() {
      this.triggerEvent("cancel", {}, {})
    },
    clear: function() {
      this.setData({
        value: '',
        showSearch: false
      })
    },
    historyClick: function(event) {
      let key = event.detail.key
      this.setData({
        value: key
      })
      this._search(key)
    },
    _search(key) {
      bookModule.search({
        q: key
      }, (res) => {
        this.setData({
          searchList: res.books,
          showSearch: true
        })
        console.log(this.data.history.indexOf(key))
        if (this.data.history.indexOf(key) == -1) {
          this.data.history.splice(0, 0, key)
        }
        wx.setStorage({
          key: "history",
          data: {
            history: this.data.history.join("-"),
          }
        })
      })
    }
  }
})